<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加采购单</title>
<script src="<%=basePath %>res/mc/js/jquery-1.10.2.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/css/icon.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/css/demo.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/lib/Hui-iconfont/1.0.7/iconfont.css" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/lib/icheck/icheck.css" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/mc/static/h-ui.admin/css/style.css" />    
<link rel="stylesheet" href="<%=basePath%>res/mc/css/jquery-ui.min.css">
<link rel="stylesheet" href="<%=basePath%>res/mc/css/ui-dialog.css">
<script type="text/javascript" src="<%=basePath%>res/js/jquery.autocomplete.min.js"></script>
<script type="text/javascript" src="<%=basePath%>res/mc/lib/layer/2.1/layer.js"></script> 
<script type="text/javascript" src="<%=basePath%>res/mc/lib/icheck/jquery.icheck.min.js"></script> 
<script type="text/javascript" src="<%=basePath%>res/mc/static/h-ui/js/H-ui.js"></script> 
<script type="text/javascript" src="<%=basePath%>res/mc/static/h-ui.admin/js/H-ui.admin.js"></script> 
<link rel="stylesheet" type="text/css" href="<%=basePath%>res/css/jquery.autocomplete.css">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="<%=basePath%>res/layui/css/layui.css">
<script src="<%=basePath%>res/layui/layui.js"></script>

<script type="text/javascript">
function del(obj) {
	var tr = obj.parentNode;
	var row = tr.rowIndex;
	$("#tab tr:eq(" + row + ")").remove();
	
}


$(function(){
	
	
	
	
	
	//同步异步
	$.ajaxSetup({
		async:false
	});	
	
	//开始加载供货商信息
	var url="<%=basePath%>beyondProcurelist/queryApply.action";
	$.post(url,null,function(info){
		for(i=0;i<info.length;i++){
			$("#applyName").append("<option value="+info[i].applyId+" name='applyName'>"+info[i].applyName+"</option>");
		}
	});
	
	
	
	//添加商品
	$("#addSp").click(function(){
		
		
		var id=$("#dosName").val();
		var number=$("#number").val();
		//alert(id+number);
		var prolistTypeName=$("#prolistTypeName").val();
		if(prolistTypeName==1){
			var url="<%=basePath%>beyondProcurelist/queryCommodityInfo.action";
			var data={"wareId":id};
			$.post(url,data,function(info){
				//alert(info.wareName);
				var wareId=info.wareId;
				//获取明细table中的数据
				var str = ""; //需要封装的字符串
				var wareName = document.getElementsByName("wareName");
				for (i = 0; i < wareName.length; i++) {
					str+= wareName[i].innerHTML;
				}
			
				var rows = $("#tab tr").length ;//表格的总行数
				if (str.indexOf(info.wareName) > -1) {
					
										
					//叠加数量
					var wareNumber= parseInt($("#"+wareId+"_wareNumber").html());
					//金额
					var wareMoney= parseInt($("#"+wareId+"_wareSumMoney").html());
					//得到叠加的数量和金额
					var sumNumber= wareNumber+ parseInt(number);
					
					var sumMoney= wareMoney+ parseInt(info.wareMoney)*number;
					
					
					//赋值
					$("#"+wareId+"_wareNumber").html(sumNumber);
					
					$("#"+wareId+"_wareSumMoney").html(sumMoney);
				} else {																																																
					//增加一行																
					var newRow="<tr class='text-c'><td>"+info.wareId+"</td><td name='wareName'>"+info.wareName+"</td><td name='wareMoney' id='wareMoney'>"+info.wareMoney+"</td><td name='wareNumber' id='"+wareId+"_wareNumber'>"+number+"</td><td id='"+wareId+"_wareSumMoney' name='sumMoney'>"+info.wareMoney*number+"</td><td class='delete_css' onclick='del(this);'><i class='layui-icon'>&#xe640;</i></td></tr>";
					$("#tab tr:last").after(newRow);
				
				}
				
				showColor();
				
			},"json");
		}else if(prolistTypeName==2){
			var url="<%=basePath%>beyondProcurelist/queryDosingInfo.action";
			var data={"dosId":id};
			$.post(url,data,function(info){
				
				var dosId=info.dosId;
				
				//获取明细table中的数据
				var str = ""; //需要封装的字符串
				var dosName = document.getElementsByName("dos");
				for (i = 0; i < dosName.length; i++) {
					str+= dosName[i].innerHTML;
				}
				
				var rows = $("#tab tr").length ;//表格的总行数
				if (str.indexOf(info.dosName) > -1) {
					
					//叠加数量
					var dosNumber= parseInt($("#"+dosId+"_dosNumber").html());
					//金额
					var dosMoney= parseInt($("#"+dosId+"_dosSumMoney").html());
					//得到叠加的数量和金额
					var sumNumber= dosNumber+ parseInt(number);
					
					var sumMoney= dosMoney+ parseInt(info.dosSalarysSalary)*number;
					
					
					//赋值
					$("#"+dosId+"_dosNumber").html(sumNumber);
					
					$("#"+dosId+"_dosSumMoney").html(sumMoney);
				} else {
					//增加一行
					var newRow="<tr class='text-c'><td>"+info.dosId+"</td><td name='dos' id='dos'>"+info.dosName+"</td><td name='dosSalarysSalary' id='dosSalarysSalary'>"+info.dosSalarysSalary+"</td><td name='dosNumber' id='"+dosId+"_dosNumber'>"+number+"</td><td name='dosSumMoney' id='"+dosId+"_dosSumMoney'>"+info.dosSalarysSalary*number+"</td><td class='delete_css' onclick='del(this);'><i class='layui-icon'>&#xe640;</i></td></tr>";
					$("#tab tr:last").after(newRow);
				}
				showColor();
				
			},"json");
		}
		 
	
	});
	
	//鼠标放上去显示颜色
	  function showColor(){
	  	
		$("#tab tr:gt(0)").hover(
			function(){$(this).addClass("hover_bg")},
			function(){$(this).removeClass("hover_bg")}
		);
	  }
	
	
	
	
	
});
	
layui.use(['form', 'layedit', 'laydate'], function(){
	  var form = layui.form
	  ,layer = layui.layer
	  ,layedit = layui.layedit
	  ,laydate = layui.laydate;
	  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	  
	  //日期
	  laydate.render({
	    elem: '#date'
	  });
	  laydate.render({
	    elem: '#date1'
	  });
	  
	  //创建一个编辑器
	  var editIndex = layedit.build('LAY_demo_editor');
	 
	 
	  
	  //监听指定开关
	  form.on('switch(switchTest)', function(data){
	    layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
	      offset: '6px'
	    });
	    layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
	  });
	  
	  
	  
	  
	  form.on('select(apply)', function(data){
		  document.getElementById("prolistTypeName").value="";
		  document.getElementById("pactName").value="";
          
          form.render('select'); //这个很重要
         
	  });
	  
	  form.on('select(prolistTypeName)', function(data){
		  
		 
		  //document.getElementById("prolistTypeName").value="";
		  var applyName=$("#applyName").val();
          $.getJSON("<%=basePath%>beyondProcurelist/queryPact.action?pactType="+data.value+"&applyId="+applyName, function(data){
              var optionstring = "";
              $.each(data.data, function(i,item){
                  optionstring += "<option value=\"" + item.pactId + "\" >" + item.pactTitle + "</option>";
              });
              $("#pactName").html('<option value=""></option>' + optionstring);
              form.render('select'); //这个很重要
          });
		  form.render('select');
          
	  });
	  
	  
	  form.on('select(pact)', function(data){
		  
		  var prolistTypeName= $("#prolistTypeName").val();
		 
		  //document.getElementById("pactName").value=select;
		  //alert(prolistTypeName); 
		  
		  var applyId=$("#applyName").val();
		  if(prolistTypeName==1){
			  
			  $.getJSON("<%=basePath%>beyondProcurelist/queryCommodity.action?pactId="+data.value, function(data){
	              var optionstring = "";
	              $.each(data.data, function(i,item){
	                  optionstring += "<option value=\"" + item.WARE_ID + "\" >" + item.WARE_NAME + "</option>";
	              }); 
	              $("#dosName").html('<option value=""></option>' + optionstring);
	              form.render('select'); //这个很重要
	          });
		  }else if(prolistTypeName==2){
			  
			 $.getJSON("<%=basePath%>beyondProcurelist/queryDos.action?pactId="+data.value, function(data){
	              var optionstring = "";
	              $.each(data.data, function(i,item){
	                  optionstring += "<option value=\"" + item.DOS_ID + "\" >" + item.DOS_NAME + "</option>";
	              });
	              $("#dosName").html('<option value=""></option>' + optionstring);
	              form.render('select'); //这个很重要
	          });
		  }
          
	  });
	  
	//监听提交
		form.on('submit(demo)', function(data){
			var str = "" ; //需要封装的字符串
			var rows = $("#tab tr").length-1 ;//表格的总行数
			
			
			//表格中所有的值.每个单元格用#隔开,每行使用&隔开.
			for(i=0;i<rows;i++){
				$("#tab tr:gt(0):eq("+i+") td").each(function(){
					str += $(this).text()+"#";
					
					
				});
				str+="&";
				   
			}
			
			
			var str=$("#str").val(str);
			
			var pactName=$("#pactName").val();
			var prolistTypeName=$("#prolistTypeName").val();
			
			var url="<%=basePath%>beyondProcurelist/add.action";
			var data=$("#my").serialize();
			$.post(url,data,function(info){
				if(info.mes=="ok"){
					var index = parent.layer.getFrameIndex(window.name);
					//重新加载主界面
					window.parent.location.reload(); //刷新父页面

					//关闭弹框
					parent.layer.close(index);
				}
			})
	
	});		
	
	
	});
	
	


</script>

<style>
  	.hover_bg{
  		background:#cccc00;
  	}
	
	#ta td{
		border:0px solid #b5d6e6;
		padding:1px;
	}
	
	#tab th,td{
		border:1px solid #b5d6e6;
		font-size:14px;
		font-weight:normal;
		text-align:center;
		vertical-align:middle;
		height:30px;
	}
	
	#tab th{
		background:#eeeeee;
	}
	
  </style>
</head>
<body>
	<form id="my" class="layui-form">
		<table id="ta">
			<tr>
			
				<td>
					<div class="layui-form-item">
						<div class="layui-input-inline">
							<select class="select" name="applyName" id="applyName" lay-filter="apply">
								<option value="" >请选择供货商</option>
							</select>
						 </div>
					 </div>
				</td>
				
				
			
				<td>
					<div class="layui-form-item">
						<div class="layui-input-inline">
							<select class="select" name="prolistTypeName" id="prolistTypeName" lay-filter="prolistTypeName">
								<option value="">请选择合同采购类型</option>
								<option value="1">商品采购</option>
								<option value="2">配料采购</option>
							</select>
						</div>
					</div>
			   </td>	
			 
			   <td>
					<div class="layui-form-item">
						<div class="layui-input-inline">
							<select class="select" name="pactName" id="pactName" lay-filter="pact">
								<option value="" >请选择有效的合同</option>
							</select>
						</div>
					</div>
			  </td>
			
			</tr>
			
			
			<tr>
				
				<td>
					<div class="layui-form-item">
						<label class="layui-form-label">药品名称:</label>
					
						<div class="layui-input-block">
							<!-- <input type="text" name="medicine" id="medicine"> -->
							<select class="select" name="dosName" id="dosName">
								
							</select>
						</div>
					</div>
				</td>
			
				
				<td> 
					<div class="layui-form-item">
						<label class="layui-form-label">数量:</label>
					
						
						<div class="layui-input-block">
							<input name="number" id="number" lay-verify="title" autocomplete="off" placeholder="请输入数量" class="layui-input" type="text">
						</div>
					</div>
					
				</td>
				
				<td style="padding-right:30px">
					<input type="button" value="增加" id="addSp" class="btn btn-info">
				</td>
			
				
			</tr>
		</table>
		
		
		<table id="tab" class="layui-table" >
			
			
			<input type="hidden" name="str" id="str">
			<tr class="text-c">
				<th>商品条形码</th>
				<th>商品名称</th>
				<th>商品单价(元)</th>
				<th>商品数量</th> 
				<th>商品总价(元)</th>
				<th>操作</th>
			</tr>
			 
		
		</table>
		
		<div>
			<tr>
			&nbsp;
			</tr>
		</div>
		
		
		<!-- <div class="layui-form-item" style="text-align:center;">
		 	<div>
		   		<button class="layui-btn" id="ok" value="提交">提交</button>
		   	</div>
		</div>
		 -->
		<div class="layui-input-block">
		        <button class="layui-btn" lay-submit="" lay-filter="demo">立即提交</button>
		</div>
	</form>
</body>
</html>